using System;
using System.Collections;
using System.Text.RegularExpressions;

namespace NCover
{
	public class Filter 
	{	
		// Not preceded with a ' = [^']
		// Not an escape slash = [^\\]
		// [^']"*?[^\]"
//		static Regex selectComments = new Regex("[^']\"" + @".*?[^\\]" + '"', RegexOptions.Singleline | RegexOptions.Compiled);
//		static Regex selectComments = new Regex("[^'](\"" + @".*?[^\\]" + "\"|\"\")", RegexOptions.Singleline | RegexOptions.Compiled);
		static Regex selectStringLiterals = new Regex("[^'](\"\"|\"" + @".*?[^\\]" + "\")", RegexOptions.Singleline | RegexOptions.Compiled);

		static Regex selectSingleLineComments = new Regex("[^']//.*", RegexOptions.Multiline | RegexOptions.Compiled);
		static Regex selectMultiLineComments = new Regex(@"[^']/\*.*?\*/", RegexOptions.Singleline | RegexOptions.Compiled);

		static Regex selectSingleLineVBComments = new Regex("[^\"]'.*", RegexOptions.Multiline | RegexOptions.Compiled);

		/// <summary>
		/// Removes comments and puts them in a reinsertable list.
		/// </summary>
		public static IList IdentifyCommentsAndStringLiterals(string original)
		{
			IList commentPositions = new ArrayList();

			MatchCollection matches = selectStringLiterals.Matches(original);

			NCover.NCoverCheck.Add(205); NCover.NCoverCheck.Add(207); NCover.NCoverCheck.Add(205); NCover.NCoverCheck.Add(204); NCover.NCoverCheck.Add(204); NCover.NCoverCheck.Add(203); NCover.NCoverCheck.Add(206); NCover.NCoverCheck.Add(205); NCover.NCoverCheck.Add(205); NCover.NCoverCheck.Add(205); NCover.NCoverCheck.Add(205); NCover.NCoverCheck.Add(205); NCover.NCoverCheck.Add(205); NCover.NCoverCheck.Add(205); foreach (Match match in matches)
			{
				commentPositions.Add(new Insert(match.Index + 1, match.Value.Substring(1)));
			}

			matches = selectSingleLineComments.Matches(original);

			NCover.NCoverCheck.Add(206); NCover.NCoverCheck.Add(208); NCover.NCoverCheck.Add(206); NCover.NCoverCheck.Add(205); NCover.NCoverCheck.Add(205); NCover.NCoverCheck.Add(204); NCover.NCoverCheck.Add(207); NCover.NCoverCheck.Add(206); NCover.NCoverCheck.Add(206); NCover.NCoverCheck.Add(206); NCover.NCoverCheck.Add(206); NCover.NCoverCheck.Add(206); NCover.NCoverCheck.Add(206); NCover.NCoverCheck.Add(206); foreach (Match match in matches)
			{
				commentPositions.Add(new Insert(match.Index + 1, match.Value.Substring(1)));
			}

			matches = selectMultiLineComments.Matches(original);

			NCover.NCoverCheck.Add(207); NCover.NCoverCheck.Add(209); NCover.NCoverCheck.Add(207); NCover.NCoverCheck.Add(206); NCover.NCoverCheck.Add(206); NCover.NCoverCheck.Add(205); NCover.NCoverCheck.Add(208); NCover.NCoverCheck.Add(207); NCover.NCoverCheck.Add(207); NCover.NCoverCheck.Add(207); NCover.NCoverCheck.Add(207); NCover.NCoverCheck.Add(207); NCover.NCoverCheck.Add(207); NCover.NCoverCheck.Add(207); foreach (Match match in matches)
			{
				commentPositions.Add(new Insert(match.Index + 1, match.Value.Substring(1)));
			}

			NCover.NCoverCheck.Add(208); NCover.NCoverCheck.Add(210); NCover.NCoverCheck.Add(208); NCover.NCoverCheck.Add(207); NCover.NCoverCheck.Add(207); NCover.NCoverCheck.Add(206); NCover.NCoverCheck.Add(209); NCover.NCoverCheck.Add(208); NCover.NCoverCheck.Add(208); NCover.NCoverCheck.Add(208); NCover.NCoverCheck.Add(208); NCover.NCoverCheck.Add(208); NCover.NCoverCheck.Add(208); NCover.NCoverCheck.Add(208); return commentPositions;
		}


		/// <summary>
		/// Removes comments and puts them in a reinsertable list.
		/// </summary>
		public static IList IdentifyVBCommentsAndStringLiterals(string original)
		{
			IList commentPositions = new ArrayList();

			MatchCollection matches = selectStringLiterals.Matches(original);

			NCover.NCoverCheck.Add(209); NCover.NCoverCheck.Add(211); NCover.NCoverCheck.Add(209); NCover.NCoverCheck.Add(208); NCover.NCoverCheck.Add(208); NCover.NCoverCheck.Add(207); NCover.NCoverCheck.Add(210); NCover.NCoverCheck.Add(209); NCover.NCoverCheck.Add(209); NCover.NCoverCheck.Add(209); NCover.NCoverCheck.Add(209); NCover.NCoverCheck.Add(209); NCover.NCoverCheck.Add(209); NCover.NCoverCheck.Add(209); foreach (Match match in matches)
			{
				commentPositions.Add(new Insert(match.Index + 1, match.Value.Substring(1)));
			}

			matches = selectSingleLineVBComments.Matches(original);

			NCover.NCoverCheck.Add(210); NCover.NCoverCheck.Add(212); NCover.NCoverCheck.Add(210); NCover.NCoverCheck.Add(209); NCover.NCoverCheck.Add(209); NCover.NCoverCheck.Add(208); NCover.NCoverCheck.Add(211); NCover.NCoverCheck.Add(210); NCover.NCoverCheck.Add(210); NCover.NCoverCheck.Add(210); NCover.NCoverCheck.Add(210); NCover.NCoverCheck.Add(210); NCover.NCoverCheck.Add(210); NCover.NCoverCheck.Add(210); foreach (Match match in matches)
			{
				commentPositions.Add(new Insert(match.Index + 1, match.Value.Substring(1)));
			}

			//No multiline comments for VB.

			return commentPositions;
		}

	}
}














